<template>
  <a-drawer width="35%" :label-col="4" :wrapper-col="14" :visible="open" @close="onClose">
    <a-divider orientation="left">
      <b>{{ formTitle }}</b>
    </a-divider>
    <a-form-model ref="form" :model="form" :rules="rules">
      <a-form-model-item label="状态" prop="status" >
        <a-select placeholder="请选择状态" v-model="form.status">
          <a-select-option v-for="(d, index) in statusOptions" :key="index" :value="d.value" >{{ d.label }}</a-select-option>
        </a-select>
      </a-form-model-item>
      <a-form-model-item label="删除标志" prop="delFlag" v-if="formType === 1">
        <a-input v-model="form.delFlag" placeholder="请输入删除标志" />
      </a-form-model-item>
      <a-form-model-item label="银行卡账号开户名" prop="buyerBankInfo" >
        <a-input v-model="form.buyerBankInfo" placeholder="请输入银行卡账号开户名" />
      </a-form-model-item>
      <a-form-model-item label="银行卡号" prop="buyerBankNo" >
        <a-input v-model="form.buyerBankNo" placeholder="请输入银行卡号" />
      </a-form-model-item>
      <a-form-model-item label="开户银行" prop="buyerBankName" >
        <a-input v-model="form.buyerBankName" placeholder="请输入开户银行" />
      </a-form-model-item>
      <a-form-model-item label="银行卡账号开户名" prop="sellerBankInfo" >
        <a-input v-model="form.sellerBankInfo" placeholder="请输入银行卡账号开户名" />
      </a-form-model-item>
      <a-form-model-item label="银行卡号" prop="sellerBankNo" >
        <a-input v-model="form.sellerBankNo" placeholder="请输入银行卡号" />
      </a-form-model-item>
      <a-form-model-item label="开户银行" prop="sellerBankName" >
        <a-input v-model="form.sellerBankName" placeholder="请输入开户银行" />
      </a-form-model-item>
      <a-form-model-item label="卖家用户id" prop="sellerUserId" >
        <a-input v-model="form.sellerUserId" placeholder="请输入卖家用户id" />
      </a-form-model-item>
      <a-form-model-item label="卖家用户名称" prop="sellerUserName" >
        <a-input v-model="form.sellerUserName" placeholder="请输入卖家用户名称" />
      </a-form-model-item>
      <a-form-model-item label="卖家用户手机号" prop="sellerUserPhone" >
        <a-input v-model="form.sellerUserPhone" placeholder="请输入卖家用户手机号" />
      </a-form-model-item>
      <a-form-model-item label="卖家确认平账" prop="sellerBalance" >
        <a-select placeholder="请选择卖家确认平账" v-model="form.sellerBalance">
          <a-select-option v-for="(d, index) in sellerBalanceOptions" :key="index" :value="d.value" >{{ d.label }}</a-select-option>
        </a-select>
      </a-form-model-item>
      <a-form-model-item label="买家用户id" prop="buyerUserId" >
        <a-input v-model="form.buyerUserId" placeholder="请输入买家用户id" />
      </a-form-model-item>
      <a-form-model-item label="买家用户名称" prop="buyerUserName" >
        <a-input v-model="form.buyerUserName" placeholder="请输入买家用户名称" />
      </a-form-model-item>
      <a-form-model-item label="买家用户手机号" prop="buyerUserPhone" >
        <a-input v-model="form.buyerUserPhone" placeholder="请输入买家用户手机号" />
      </a-form-model-item>
      <a-form-model-item label="买家确认平账" prop="buyerBalance" >
        <a-select placeholder="请选择买家确认平账" v-model="form.buyerBalance">
          <a-select-option v-for="(d, index) in buyerBalanceOptions" :key="index" :value="d.value" >{{ d.label }}</a-select-option>
        </a-select>
      </a-form-model-item>
      <a-form-model-item label="打款金额" prop="amount" >
        <a-input v-model="form.amount" placeholder="请输入打款金额" />
      </a-form-model-item>
      <div class="bottom-control">
        <a-space>
          <a-button type="primary" :loading="submitLoading" @click="submitForm">
            保存
          </a-button>
          <a-button type="dashed" @click="cancel">
            取消
          </a-button>
        </a-space>
      </div>
    </a-form-model>
  </a-drawer>
</template>

<script>
import { getQdPay, addQdPay, updateQdPay } from '@/api/system/qdPay'

export default {
  name: 'CreateForm',
  props: {
    statusOptions: {
      type: Array,
      required: true
    },
    sellerBalanceOptions: {
      type: Array,
      required: true
    },
    buyerBalanceOptions: {
      type: Array,
      required: true
    }
  },
  components: {
  },
  data () {
    return {
      submitLoading: false,
      formTitle: '',
      // 表单参数
      form: {
        id: null,

        status: null,

        delFlag: null,

        createBy: null,

        createTime: null,

        updateBy: null,

        updateTime: null,

        buyerBankInfo: null,

        buyerBankNo: null,

        buyerBankName: null,

        sellerBankInfo: null,

        sellerBankNo: null,

        sellerBankName: null,

        sellerUserId: null,

        sellerUserName: null,

        sellerUserPhone: null,

        sellerBalance: null,

        buyerUserId: null,

        buyerUserName: null,

        buyerUserPhone: null,

        buyerBalance: null,

        amount: null

      },
      // 1增加,2修改
      formType: 1,
      open: false,
      rules: {
      }
    }
  },
  filters: {
  },
  created () {
  },
  computed: {
  },
  watch: {
  },
  mounted () {
  },
  methods: {
    onClose () {
      this.open = false
    },
    // 取消按钮
    cancel () {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset () {
      this.formType = 1
      this.form = {
        id: null,

        status: null,

        delFlag: null,

        createBy: null,

        createTime: null,

        updateBy: null,

        updateTime: null,

        buyerBankInfo: null,

        buyerBankNo: null,

        buyerBankName: null,

        sellerBankInfo: null,

        sellerBankNo: null,

        sellerBankName: null,

        sellerUserId: null,

        sellerUserName: null,

        sellerUserPhone: null,

        sellerBalance: null,

        buyerUserId: null,

        buyerUserName: null,

        buyerUserPhone: null,

        buyerBalance: null,

        amount: null

      }
    },
    /** 新增按钮操作 */
    handleAdd (row) {
      this.reset()
      this.formType = 1
      this.open = true
      this.formTitle = '添加'
    },
    /** 修改按钮操作 */
    handleUpdate (row, ids) {
      this.reset()
      this.formType = 2
      const id = row ? row.id : ids
      getQdPay(id).then(response => {
        this.form = response.data
        this.open = true
        this.formTitle = '修改'
      })
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.submitLoading = true
          if (this.form.id !== undefined && this.form.id !== null) {
            updateQdPay(this.form).then(response => {
              this.$message.success(
                '修改成功',
                3
              )
              this.open = false
              this.$emit('ok')
            }).finally(() => {
              this.submitLoading = false
            })
          } else {
            addQdPay(this.form).then(response => {
              this.$message.success(
                '新增成功',
                3
              )
              this.open = false
              this.$emit('ok')
            }).finally(() => {
              this.submitLoading = false
            })
          }
        } else {
          return false
        }
      })
    }
  }
}
</script>
